//
//  BannerView.m
//  FlyGift
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BannerView.h"


@interface BannerView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(nonatomic, strong)NSMutableArray *bannerModelArray;
@end

@implementation BannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self bannerDataRequest];
        
        UILabel *bannerLabel = [[UILabel alloc]initWithFrame:CGRectMake(kP6width(10), kP6Height(10), kP6width(100), kP6Height(30))];
        bannerLabel.text = @"专题";
        
        self.moreButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.moreButton.frame = CGRectMake(frame.size.width - kP6width(110), kP6Height(10), kP6width(110), kP6Height(30));
        [self.moreButton setTitle:@"查看全部>" forState:UIControlStateNormal];
        // 点击按钮跳转到全部专题页面
        [self.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.moreButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.bannerCollections = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kP6Height(42), kScreenWidth, kP6Height(120)) collectionViewLayout:flowLayout];
        self.bannerCollections.delegate = self;
        self.bannerCollections.dataSource = self;
        self.bannerCollections.backgroundColor = [UIColor whiteColor];
        self.bannerCollections.showsHorizontalScrollIndicator = NO;
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kP6Height(41), frame.size.width, 1)];
        view.backgroundColor = KColor(242, 243, 238, 1);
        [self addSubview:view];
        
        UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, kP6Height(162), frame.size.width, 1)];
        view1.backgroundColor = KColor(242, 243, 238, 1);
        [self addSubview:view1];
        
        [self addSubview:bannerLabel];
        [self addSubview:_moreButton];
        [self addSubview:_bannerCollections];
        
    }
    return self;
}


#pragma mark - 点击按钮跳转到全部专题页面
- (void)moreButtonAction:(UIButton *)button
{
    if (self.moreButtonBlock == nil)
    {
        return;
    }
    self.moreButtonBlock(button);
}



#pragma mark - UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 15, 10);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kP6width(180), kP6Height(88));
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *bannerCell = @"BannerCell";
    [collectionView registerClass:[BannerCollectionViewCell class] forCellWithReuseIdentifier:bannerCell];
    BannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:bannerCell forIndexPath:indexPath];
    BannerModel *model = self.bannerModelArray[indexPath.row];
    [cell.bannerImageView setImageWithURL:[NSURL URLWithString:model.banner_image_url] placeholderImage:[UIImage imageNamed:@"300 * 147"]];
    return cell;
}

#pragma mark- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BannerModel *model = self.bannerModelArray[indexPath.row];
    
    if (self.clickItemBlock == nil)
    {
        return;
    }
    self.clickItemBlock(model.bannerId);
}



#pragma mark - 数据部分
- (void)bannerDataRequest
{
    [LORequestManger GET:BannerURL success:^(id response) {
        if (response == nil)
        {
            return;
        }
        NSDictionary *dictionary = (NSDictionary *)response;
        NSDictionary *dataDic = dictionary[@"data"];
        NSArray *collectionsArray = dataDic[@"collections"];
        self.bannerModelArray = [NSMutableArray array];
        for (NSDictionary *dic in collectionsArray)
        {
            BannerModel *model = [[BannerModel alloc]init];
            model.banner_image_url = dic[@"banner_image_url"];
            model.bannerId = [dic[@"id"] stringValue];
            [self.bannerModelArray addObject:model];
        }
        [self.bannerCollections reloadData];
    } failure:nil];
}
@end

















